function project_build()
{
	return project.build();
}

function project_new()
{
	var a = ensureSave->(); 
	if (!a)
		return false;
	// TODO: ask project name
	project = new Project();
	return true;
}

function project_open()
{
	var a = ensureSave->(); 
	if (!a)
        return false;
    var pd = getCurrentDir() + 'projects/';
    
    var file = chooseFile->({
        dir: pd,
        title: 'Choose the file or enter its name',
        selectButtonText: 'Open',
        test: function(x) {return fileExists(pd + x + '/' + x + '.sbp')}
    });
    if (!file)
        return false;
	if (!project)
		project = new Project();
    var res = project.load('file://' + pd + file + '/' + file + '.sbp');
    if (res)
        loadProject(project, canvas);
    return Boolean(res);
}

function project_save()
{
    if (project && project._file)
    {
    	var res = true;
        if (project._modified)
            res = project.save('file://' + project._file)
        $('h1:first span').text(project.displayTitle());
    	return Boolean(res);
    }
    return false;
}

function project_saveAs()
{
    if (!project)
        return true;
    var pd = getCurrentDir() + 'projects/';

    var file = chooseFile->({
        dir: pd,
        title: 'Choose file',
        selectButtonText: 'Save',
		manual: true,
        test: function(x) {return fileExists(pd + x + '/' + x + '.sbp')},
    });
    if (!file)
        return false;
    var res = project.save('file://' + pd + file + '/' + file + '.sbp');
    $('h1:first span').text(project.displayTitle());
	return Boolean(res);
}

function project_close(canvas)
{
	var a = ensureSave->(); 
	if (!a)
		return false; 
	project = null;
	loadProject(project, canvas);
    return true;
}

function ensureSave()
{
	if (!project || !project.needsSave())
		return true;
	var a = getConfirmation->();
	if (a == true)
		if (project._file)
			return project_save->();
		else
			return project_saveAs->();
	else if (a == false)
		return true;
	return false;
}

function mockup_add()
{}

function mockup_delete()
{
	if (!currentMockup)
		return true;
	project.deleteMockup(currentMockup.id);
	loadMockup(null, canvas);
}

function mockup_clear()
{
	clearMockup(canvas);
}

